﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using NeuronDotNet.Samples.OCR.layoutsstategies;

// Stefan Bojarovski
// november 2011
namespace NeuronDotNet.Controls
{
  public class DivisionBuilder : LayoutStrategy
  {
    protected List<Rectangle> letterDivision;
    Letter letter;

    public DivisionBuilder(Rectangle imageRect, Letter letter)
            : base(imageRect)
    {
        this.letter = letter; 
    }

    #region added methods
    private List<Rectangle> ScanMap(Bitmap map)
    {
      List<Rectangle> result = new List<Rectangle>();
      int start = 0;
      int width = 0;
      bool previousWhite = true;

      //In every column
      for (int i = 0; i < map.Width; i++)
      {
        bool isWhite = true;
        //Scan the pixels
        for (int j = 0; j < map.Height; j++)
        {
          //If we meet a colored pixel
          if ((map.GetPixel(i, j).R != 255) && (map.GetPixel(i, j).G != 255) && (map.GetPixel(i, j).B != 255))
          {
            //found a new letter
            isWhite = false;
            previousWhite = false;
            width++;
            //continue to next column
            break;
          }
          //if we reach this, isWhite is true
        }
        //End of letter
        if (((isWhite) && (!previousWhite))
            || ((i == map.Width - 1) && (!previousWhite)))
        {
          start = i - width;
          result.Add(new Rectangle(start + letter.left, letter.top, width, map.Height));
          width = 0;
          previousWhite = true;
        }
      }
      return result;
    }

    public override List<Rectangle> GetDivision()
    {
        Bitmap map = letter.MakeBitmap();
        map.Save("letter.bmp");
        //Scan the bitmap and create a list of borders
        return ScanMap(map);      
    }

    #endregion    
  }
}
